﻿using Gp.Scripts.Core;
using Gp.Scripts.Data;
using Sirenix.OdinInspector;
using Sirenix.Utilities;
using UnityEngine.Serialization;

namespace Gp.Scripts.Database {
    [GlobalConfig("Assets/AAA/Config")]
    public class SystemDataBase : GlobalConfig<SystemDataBase> {
        public new static SystemDataBase Instance =>
# if UNITY_EDITOR
            GlobalConfig<SystemDataBase>.Instance;
# else
            SystemDatabaseLoader.Instance.GetConfig<SystemDataBase>();
# endif
        
        // public InventoryConfigObject errorObject;
        
        
        [ReadOnly] [ListDrawerSettings(ShowFoldout = true)]
        public UnitPrefabEntityConfig[] allCharacters;

        // [FormerlySerializedAs("allEquips")] 
        // [ReadOnly] 
        // [ListDrawerSettings(ShowFoldout = true)]
        // public InventoryConfigObject[] allInventoryItems;
        
        
        [ReadOnly] [ListDrawerSettings(ShowFoldout = true)]
        public EntityConfigObject[] dataObjects;


#if UNITY_EDITOR
        [Button("更新数据", ButtonSizes.Medium), PropertyOrder(-1)]
        public void UpdateCharacterOverview() {
            // this.allCharacters = AssetDatabase.FindAssets($"t:{nameof(UnitPrefabConfig)}")
            //     .Select(guid => AssetDatabase.LoadAssetAtPath<UnitPrefabConfig>(AssetDatabase.GUIDToAssetPath(guid)))
            //     .ToArray();
            //
            // this.allInventoryItems = AssetDatabase.FindAssets($"t:{nameof(InventoryConfigObject)}")
            //     .Select(guid => AssetDatabase.LoadAssetAtPath<InventoryConfigObject>(AssetDatabase.GUIDToAssetPath(guid)))
            //     .ToArray();
        }


        [Button("同步库存物品文件名", ButtonSizes.Medium)]
        [PropertyOrder(-1)]
        public void UpdateInventoryItemName() {
            // foreach (var item in allInventoryItems) {
            //     AssetDatabase.RenameAsset(AssetDatabase.GetAssetPath(item), item.ItemName);
            // }
            //
            // foreach (var item in dataObjects) {
            //     AssetDatabase.RenameAsset(AssetDatabase.GetAssetPath(item), item.ItemName);
            // }
            //
            //
            //
            // AssetDatabase.SaveAssets();
            // AssetDatabase.Refresh();
        }

        
        [Button("更新资源", ButtonSizes.Large), PropertyOrder(-1)]
        private void UpdateConfigAssets() {
        //     var allData = new HashSet<Type>(typeof(ConfigObject).Assembly.GetTargetType(baseClass: typeof(ConfigObject))
        //         .Where(t => t.GetCustomAttribute<IgnoreCreateAssetAttribute>() == null));
        //     
        //     var curData = AssetDatabase.FindAssets($"t:{nameof(ConfigObject)}")
        //         .Select(guid => AssetDatabase.LoadAssetAtPath<ConfigObject>(AssetDatabase.GUIDToAssetPath(guid)))
        //         .ToArray();
        //     
        //     foreach (var o in curData) {
        //         allData.Remove(o.GetType());
        //     }
        //
        //     foreach (var t in allData) {
        //         var obj = CreateInstance(t) as ConfigObject;
        //         var path = obj.FilePath();
        //         path = path.Trim('/');
        //
        //         if (string.IsNullOrEmpty(path)) {
        //             path = "Misc";
        //         }
        //
        //         string rootPath = @"Assets/AAA/Data/" + path;
        //         if (!Directory.Exists(rootPath)) {
        //             Directory.CreateDirectory(rootPath);
        //         }
        //         AssetDatabase.CreateAsset(obj,  rootPath + "/" + obj.FileName() + ".asset");
        //         AssetDatabase.SaveAssets();
        //     }
        //     
        //     dataObjects =  AssetDatabase.FindAssets($"t:{nameof(ConfigObject)}")
        //         .Select(guid => AssetDatabase.LoadAssetAtPath<ConfigObject>(AssetDatabase.GUIDToAssetPath(guid)))
        //         .ToArray();
        //     
        //     EditorUtility.SetDirty(this);
        //     
        //     
        //     AssetDatabase.Refresh();
        } 
#endif
    }

}